<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/4/16
 * Time: 16:37
 */
namespace Assistant\Controller;
use Admin\Controller\AdminController;

class AccountController extends AdminController
{
    //账单列表
    public function lst(){
//        $year = D('Admin/Dict')->getDict('year');
        $data = D('Account')->accountSearch();
//        echo '<pre>';
//        var_dump(I('param.'));
//        var_dump($data);die;
        $assign = array(
            'page' => $data['page'],
            'data' => $data['data']
        );

        $this->assign($assign);
        $this->display();
    }
    //账单管理
    public function manage(){
        if(IS_POST) {
            $model = D('Account');
            $is_ok = $model->pay();
            if($is_ok)
                $this->success('操作成功!');
            else
                $this->error('金额出错!');
            exit();
        }
        $payWay = D('Admin/Dict')->getDict('payWay');
        $model = D('Account');
        $data = $model->detail();
        $this->assign('data', $data);
        $this->assign('payWay', $payWay);
//        echo '<pre>';
//        var_dump($data);die;
        $this->display();
    }
    //生成账单
    public function createAccount(){
        $expenseId = I('param.expense_id');
        $roomId = I('param.room_id');

        $model = D('Account');
        $id = $model->checkUnique($roomId);
        if($id)
            $this->error('此房间有账单未缴清，无法生成账单！');
         if($expenseId)
    	{
            $sign = $model->createAccount($expenseId, $roomId);
            if($sign['status']==1)
            {
              $this->success('账单生成成功！',U('Expense/adding',array('room_id'=>$roomId)),1);exit();
//                    $data['status'] = 1;
//                    $this->ajaxReturn($data); // 成功
            }
            $this->error($sign['error']);
//            $data['status'] = 0;
//            $data['sign'] = ($model->getError());
//            $this->ajaxReturn($data);
        }
        $this->error('请选择费用');
    }
    
    //批量生成账单
    public function createAll(){
        $data = D('Expense')->expenseSearch(999999,false,1,0)['data'];
        foreach ($data as $k=>$v) {
            $expenseArr[$v['room_id']][] = $v['id'];
        }
//        myDump($data);
//        myDump($expenseArr);die;
        foreach ($expenseArr as $k=>$v) {
            $sign = D('Account')->createAccount($v, $k);
            if($sign['status']==0)
                $this->error($sign['error']);
        }
        $this->success('账单生成成功！',U('Expense/lst'));
    }

    public function delete()
    {
        $id = I('id');
        $room_id = I('room_id');
        $account = D('Account')->where(array('id'=>$id,'pay_money'=>array('lt',0.01)))->find();
        $expense_id = explode(',',$account['expense_id']);
        foreach ($expense_id as $v) {
            $data['id'] = $v;
            $data['is_edit'] = 1;
            D('Expense')->data($data)->save();
        }
        $wx_order = M('Wyf','tp_','DB_WX')->where(array('crm_wid'=>$id))->find();
        $ok2 = M('Wyforder','tp_','DB_WX')->where(array('outid'=>$wx_order['id']))->select();
//        myDump($wx_order);
//        myDump($ok2);die;
        $ok1 = M('Wyforder','tp_','DB_WX')->where(array('outid'=>$wx_order['id']))->delete();
        $ok2 = M('Wyf','tp_','DB_WX')->where(array('crm_wid'=>$id))->delete();
        $ok3 = D('Account')->delete($id);
        $this->success('账单已经取消，请重新生成！');
    }

    public function logLst(){
        $data = D('Admin/Finance')->ExpenseOrder(10,1);
//        $data = D('Account')->searchLog(10,1);
        $payWay = D('Admin/Dict')->getDict('payWay');
        $assign = array(
            'data'=>$data['data'],
            'page'=>$data['page'],
            'payWay'=>$payWay,
            'start_time'=>I('start_time'),
            'end_time'=>I('end_time'),
        );
        $this->assign($assign);
        $this->display();
    }
    
    public function dumpExcel(){
        D('Account')->dumpExcel();
    }


}